package com.yuqian.JavaSe.leetcode.easy;

/**
 * Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

不用额外的空间检查一个链表是否存在环
 * @author yuqian
 *
 */
public class LinkedListCycle {
	 public boolean hasCycle(ListNode head) {
	        if(head==null||head.next==null){
	            return false;
	        }
	        ListNode next =null;
	        //如果一个节点没有指向自己就判断下一个节点，同时把当前节点指向自己
	        while(head.next!=null){
	            if(head.next==head){
	                return true;
	            }else{
	                next = head.next;
	                head.next =head;
	                head =next;
	            }
	        }
	        return false;
	    }
}
